package com.hhly.rabbitmq.topic;

import java.io.IOException;

import com.hhly.rabbitmq.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class ReceiveLogsTopic {
	
   public static void main(String[] args) throws Exception {
	   receive("warning.#");
	   receive("*.error");
   }
   
   public static  void  receive(final String routing) throws Exception{
  	 Channel channel = RabbitMQUtil.getChannel();
  	 channel.exchangeDeclare(EmitLogTopic.EXCHANGE_NAME, "topic");
  	 String queueName = channel.queueDeclare().getQueue();
  	 channel.queueBind(queueName, EmitLogTopic.EXCHANGE_NAME, routing);
  	 Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
					byte[] body) throws IOException {
				String message = new String(body, "UTF-8");
				System.out.println(routing+" [x] Received '" + message + "'");
			}
		};
		channel.basicConsume(queueName, true, consumer);
  }
}
